<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Library\Models\AdminShopProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ShopProductDiscountAction extends RowAction
{
    public $name = '优惠时间';

    /**
     * handle
     * @param AdminShopProductModel $model
     * @param Request $request
     * @return Response
     */
    public function handle(AdminShopProductModel $model, Request $request): Response
    {
        $model->product_special_start_time = $request->startAt;
        $model->product_special_end_time = $request->endAt;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.discountAction');
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * handle
     * @param AdminShopProductModel $model
     * @return void
     */
    public function form(AdminShopProductModel $model): void
    {
        $this->datetime('startAt', '优惠开始')->default($model->product_special_start_time);
        $this->datetime('endAt', '优惠结束')->default($model->product_special_end_time);
    }
}
